<script lang="ts" setup>
import { ref, onMounted, computed } from 'vue'
import {
  getPatientList,
  addPatient,
  editPatient,
  delPatient,
} from '@/services/user'
import type { PatientList, Patient } from '@/types/user'
import {
  type FormInstance,
  showToast,
  showSuccessToast,
  showConfirmDialog,
} from 'vant'
import { nameRules, idcardRules } from '@/utils/rules'
onMounted(() => {
  getList()
})
// 获取患者列表
const list = ref<PatientList>()
const getList = async () => {
  const res = await getPatientList()
  list.value = res.data
}

const show = ref(false)
const initPatient: Patient = {
  name: '',
  idCard: '',
  defaultFlag: 0,
  gender: 1,
}
const patient = ref<Patient>({ ...initPatient })
const options = [
  { label: '男', value: 1 },
  { label: '女', value: 0 },
]
// 打开侧边栏
const showPopUp = (item?: Patient) => {
  if (item) {
    const { id, defaultFlag, gender, idCard, name } = item
    patient.value = { id, defaultFlag, gender, idCard, name }
  } else {
    patient.value = { ...initPatient }
  }
  show.value = !show.value
}
// 默认选中的计算属性
const defaultFlag = computed({
  get() {
    return patient.value.defaultFlag === 1 ? true : false
  },

  set(val) {
    patient.value.defaultFlag = val ? 1 : 0
  },
})
const formRef = ref<FormInstance>()

// 保存填写的数据
const save = async () => {
  // 表单校验
  await formRef.value?.validate()
  // 校验身份证倒数第二位 奇数男 偶数女
  const gender = +patient.value.idCard.slice(-2, -1) % 2
  if (gender !== patient.value.gender) {
    return showToast('选择的性别和身份证中包含的性别不一致, 请修改~')
  }
  patient.value.id
    ? await editPatient(patient.value)
    : await addPatient(patient.value)

  show.value = false

  getList()
  showSuccessToast(`${patient.value.id ? '编辑成功' : '添加成功'}`)
}

const del = async () => {
  // 4.1 判断是否有id
  if (patient.value.id) {
    // 4.2 用户确认
    await showConfirmDialog({
      title: '温馨提示',
      message: `确定删除 ${patient.value.name} 患者吗?`,
    })
    // 4.3 调用删除接口
    await delPatient(patient.value.id)
    // 4.4 关闭弹窗
    show.value = false
    // 4.5 重新拉取列表数据
    getList()
    // 4.6 提示用户
    showSuccessToast('删除成功~')
  }
}
</script>

<template>
  <div class="patient-page">
    <CpNavBar title="家庭档案"></CpNavBar>
    <div class="patient-list">
      <div class="patient-tip">最多可添加6人</div>
      <div class="patient-item" v-for="item in list" :key="item.id">
        <div class="info">
          <div class="default-id">
            <div class="name">{{ item.name }}</div>
            <span class="id">{{
              item.idCard.replace(/^(.{6}).+(.{4})$/, '\$1********\$2')
            }}</span>
            <span class="default" v-if="item.defaultFlag === 1">默认</span>
          </div>

          <div class="sex-age">
            <span>{{ item.genderValue }}</span>
            <span>{{ item.age }}</span>
          </div>
        </div>

        <div class="icon" @click="showPopUp(item)">
          <cp-icon name="user-edit" />
        </div>
      </div>
      <div class="patient-add" @click="showPopUp()">
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
    </div>

    <van-popup v-model:show="show" position="right">
      <!-- 导航条 -->
      <cp-nav-bar
        :title="patient.id ? '编辑患者' : '添加患者'"
        right-text="保存"
        :back="() => (show = false)"
        @right-click="save"
      ></cp-nav-bar>
      <!-- 表单 -->
      <van-form autocomplete="off" ref="formRef">
        <van-field
          v-model="patient.name"
          label="真实姓名"
          placeholder="请输入真实姓名"
          :rules="nameRules"
        ></van-field>
        <van-field
          v-model="patient.idCard"
          label="身份证号"
          placeholder="请输入身份证号"
          :rules="idcardRules"
        ></van-field>
        <van-field label="性别">
          <template #input>
            <cp-radio-btn
              v-model="patient.gender"
              :options="options"
            ></cp-radio-btn>
          </template>
        </van-field>
        <van-field label="默认就诊人">
          <template #input>
            <van-checkbox icon-size="18px" v-model="defaultFlag"></van-checkbox>
          </template>
        </van-field>
      </van-form>
      <!-- 删除动作按钮 -->
      <van-action-bar v-if="patient.id">
        <van-action-bar-button @click="del">删除</van-action-bar-button>
      </van-action-bar>
    </van-popup>
  </div>
</template>

<style lang="scss" scoped>
.patient-page {
  padding: 46px 0 80px;
  :deep() {
    .van-popup {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
    }
  }
}

.patient-list {
  padding: 15px;
}

.patient-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  border: 1px solid var(--cp-bg);
  padding: 20px 15px;
  /* 下面的元素好像没有什么作用 */
  position: relative;
  transition: all 0.3s;
  overflow: hidden;

  .info {
    .default-id {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .name {
        width: 100px;
        font-size: 16px;
        color: var(--cp-text1);
      }
      .default {
        width: 30px;
        height: 16px;
        margin-right: 10px;
        font-size: 10px;
        color: #fff;
        background-color: var(--cp-primary);
        border-radius: 2px;
        display: inline-block;
        text-align: center;
      }
      .id {
        color: var(--cp-tag);
        font-size: 12px;
        margin: 0 10px;
      }
    }

    .sex-age {
      margin-top: 5px;
      font-size: 12px;
      color: var(--cp-tag);
      span:first-child {
        margin-right: 25px;
      }
    }
  }

  .icon {
    color: var(--cp-primary);
    width: 20px;
    text-align: center;
  }
}

.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}

.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.pb4 {
  padding-bottom: 4px;
}
.van-action-bar {
  padding: 0 10px;
  margin-bottom: 10px;
  .van-button {
    color: var(--cp-price);
    background-color: var(--cp-bg);
  }
}
</style>
